layui.use(['element', 'table', 'form'], function () {
        var element = layui.element;
        var $ = layui.$;
        var form = layui.form;
        var table = layui.table;

        var jsProduct = {
            'result':'1',
        }
        getTable(jsProduct);
        function getTable(jsProduct) {
            $.ajax({
                url: '/login/getUser',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    var id=data.data.userId;
                    jsProduct.userId=id;
                    console.log(id);
                    console.log(jsProduct);
                    var userId=localStorage.getItem("createId");
                    jsProduct.updateId=userId;
                    $.ajax({
                        url: '/courseTable/getTable',
                        type: 'post',
                        dataType: 'json',
                        data: jsProduct,
                        success: function (data) {
                            tablereload(data.data);
                        }
                    });
                }
            });
        }

        function tablereload(date) {
            table.render({
                elem: '#table',
                data: date,
                title: '数据列表',
                limit: 10,
                id: 'table',
                toolbar: '#toptool',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                    first: "首页",
                    last: "尾页",
                },
                size: 'lg',
                cols: [[//定义列
                    {type: 'checkbox'},//复选框靠左
                    {field: 'courseId', title: '商品编号', align: 'center'},
                    {
                        field: "url", title: "商品图片", align: 'center', templet: function (data) {
                            return "<img src='/images/goods-img/" + data.url + "'/>";
                        }
                    },
                    {field: 'coursePrimary', title: "课程名称", align: 'center'},
                    {field: 'coursePrimary', title: "关键字", align: 'center'},
                    {
                        field: 'courseNprice',
                        title: "课程价格",
                        align: 'center',
                        templet: function (data) {
                            return "￥" + data.courseNprice;
                        }
                    },
                    {
                        field: 'courseStatus',
                        title: "上架/下架",
                        align: 'center',
                        templet: function (data) {
                            if(data.courseStatus==2){
                                var radio = '<a href="javascript:updateStatus('+data.courseId+","+data.courseStatus+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上架|下架" lay-filter="switchTest" value="'+data.courseStatus+'" checked></a>'
                            }else{
                                var radio = '<a href="javascript:updateStatus('+data.courseId+','+data.courseStatus+');"><input class="layui-input swi" type="checkbox" name="sate" lay-skin="switch" lay-text="上架|下架" lay-filter="switchTest" value="'+data.courseStatus+'"></a>'
                            }
                            return radio;
                        }
                    },
                    {field: 'title', title: "商品分类", align: 'center'},
                    {field: 'remarks', title: "审核状态", align: 'center',
                        templet: function (data) {
                            if(data.courseCstatus==2){
                                var radio = '<p>审核通过</p>'
                            }else if(data.courseCstatus==3){
                                var radio = '<p>待审核</p>'
                            }else if(data.courseCstatus==4){
                                var radio = '<p>审核中</p>'
                            }else if(data.courseCstatus==5){
                                var radio = '<p>审核失败</p>'
                            }else{
                                var radio = '<p>未知</p>'
                            }
                            return radio;
                        }
                    },
                    {field: 'saleNum', title: "销量", align: 'center'},
                    {
                        field: '', title: "操作", align: 'center', templet: function (data) {
                            return "<div>" +
                                "<a href='javascript:getProduct(" + data.courseId + ");' style='color: #0EAA9E;margin-right: 10px'>查看</a>" +
                                "<a href='javascript:delCourse(" + data.courseId + ");' style='color: #0EAA9E'>删除</a>" +
                                "</div>";
                        }
                    },
                ]],
            });
        }
        gCha = function gCha(){
            getTable();
        }

        shangJia = function shangJia(){
            var courseStatus=2;
            var param={
                "courseStatus":courseStatus
            }
            getTable(param);
        }

        weiJia = function weiJia() {
            var courseStatus = 3;
            var param={
                "courseStatus":courseStatus
            }
            getTable(param);
        }

        dShen = function dShen() {
            var remarks=3;
            var param={
                "remarks":remarks
            }
            getTable(param);
        }

        Bhui = function Bhui(){
            var remarks=5;
            var param={
                "remarks":remarks
            }
            getTable(param);
        }







        //搜索
        search = function search() {
            var productName = $('input[name=productName]').val();
            var courseNprice = $('input[name=courseNprice]').val();

            var classification = document.getElementById("classId");
            var indexClass = classification.selectedIndex;
            var classId = classification.options[indexClass].value;

            // var state = document.getElementById("productSate");
            // var indexState = state.selectedIndex;
            // var productSate = state.options[indexState].value;
            var productLabel = $('input[name=productLabel]').val();
            var param = {
                'coursePrimary':productName,
                'classId':classId,
                "courseNprice":courseNprice
                //'productSate':productSate
            }
            getTable(param);
        }
        //删除商品
        delCourse = function delCourse(id) {
            $.ajax({
                url: '/courseTable/delCourse',
                type: 'post',
                dataType: 'json',
                data: {'id': id},
                success: function (data) {
                    if (data) {
                        layer.msg("删除成功");
                        getTable();
                    } else {
                        layer.msg("删除成功");
                    }
                }
            });
        }
        //查看当前商品的日志
        getCoursejournal = function getCoursejournal(courseId){
            $.ajax({
                url: "/courseTable/getCoursejournal",
                type: "post",
                dataType: "json",
                data: {"courseId": courseId},
                success: function (data) {
                    localStorage.setItem("journalId",courseId);
                    layer.open({
                        type: 2,
                        title: "详情",
                        area: ['1200px', '700px'],
                        shade: 0,
                        offset: 't',
                        content: "/course/course-journal.html",
                        success: function (layero, index) {

                        }
                    });
                },
            })
        }

        //查看商品详情
        getProduct = function getProduct(courseId) {
            console.log(courseId);
            $.ajax({
                url: "/courseTable/getCourse",
                type: "post",
                dataType: "json",
                data: {"courseId": courseId},
                success: function (data) {
                    devicesMsg(data);
                },
            })
        }

        function devicesMsg(data) {
            var userName="";
            var userAcc="";
            console.log("执行页面"+data.data);
            layer.open({
                type: 2,
                title: "详情",
                area: ['1200px', '700px'],
                shade: 0,
                offset: 't',
                content: "/course/course-see.html",
                success: function (layero, index) {
                    $.ajax({
                        url: "/login/getUser",
                        type: "post",
                        dataType: "json",
                        success: function (user) {
                            userName=user.data.trueName;
                            userAcc=user.data.account;
                            console.log(userName,userAcc);
                            var obj = layero.find('iframe').contents();
                            obj.find("#productCode").html(data.data.courseName);
                            obj.find("#productName").html(data.data.createDate);
                            obj.find("#currentpriceProduct").html(data.data.userName);

                            obj.find("#xiaoliang").html(data.data.title);
                            obj.find("#title").html(data.data.courseNprice);

                            obj.find("#productLabel").html(data.data.likeNum);
                            obj.find("#hits").html(data.data.collectNum);
                            obj.find("#productDescription").html(data.data.courseIntro);
                            obj.find("#beizhu").html(data.data.courseInfo);
                            obj.find("#mainImage").attr('src', '/images/goods-img/' + data.data.url);
                            obj.find("#Image").attr('src', '/images/course-img/' + data.data.courseUrl);
                            obj.find("#name").html('用户名：<input type="text"  value="'+userName+'">');
                            obj.find("#acc").html('账号：<input type="text"  value="'+userAcc+'">');
                        },
                    })

                }
            });
        }

        /*表格详情结束*/
        //开关事件
        updateStatus = function updateStatus( id,courseStatus) {
            if(courseStatus==2){
                courseStatus=3;
            }else{
                courseStatus=2;
            }
            $.ajax({
                url: '/courseTable/updateCourseStatus',
                type: 'post',
                dataType: 'json',
                data: {
                    'id': id,
                    'courseStatus': courseStatus
                },
                success:function (data) {
                    layer.msg("修改成功");
                }
            })
        }

        //查看当前商品的审核
        courseCstatus = function courseCstatus(courseId,courseCstatus){
            $.ajax({
                url: '/courseTable/getRecord',
                type: 'post',
                dataType: 'json',
                data: {
                    'courseId': courseId
                },
                success: function (data) {
                    localStorage.setItem("recId",courseId);
                    layer.open({
                        type: 2,
                        title: "详情",
                        area: ['1200px', '700px'],
                        shade: 0,
                        offset: 't',
                        content: "/course/course-record.html",
                        success: function (layero, index) {

                        }
                    });
                },
            })
        }





        /*表格修改开始*/
        updateCourse = function updateCourse(courseId) {
            layer.open({
                type: 2,
                title: "修改",
                area: ['1200px', '700px'],
                shade: 0,
                offset: 't',
                content: "/course/course-update.html",
                success: function (layero, index) {
                    var obj = layero.find('iframe').contents();
                    $.ajax({
                        url: "/courseTable/getCourse",
                        type: "post",
                        dataType: "json",
                        data: {"courseId": courseId},
                        success: function (data) {
                            $.ajax({
                                url: "/login/getUser",
                                type: "post",
                                dataType: "json",
                                success: function (user) {
                                    userName=user.data.trueName;
                                    userAcc=user.data.account;
                                    console.log(userName,userAcc);
                                    var obj = layero.find('iframe').contents();
                                    obj.find("#productCode").val(data.data.courseName);
                                    obj.find("#productName").val(data.data.createDate);
                                    obj.find("#currentpriceProduct").val(data.data.userName);

                                    obj.find("#xiaoliang").val(data.data.title);
                                    obj.find("#title").val(data.data.courseNprice);

                                    obj.find("#productLabel").val(data.data.likeNum);
                                    obj.find("#hits").val(data.data.collectNum);
                                    obj.find("#productDescription").val(data.data.courseIntro);
                                    obj.find("#beizhu").val(data.data.courseInfo);
                                    obj.find("#mainImage").attr('src', '/images/goods-img/' + data.data.url);
                                    obj.find("#Image").attr('src', '/images/course-img/' + data.data.courseUrl);
                                    obj.find("#name").html('用户名：<input type="text"  value="'+userName+'">');
                                    obj.find("#acc").html('账号：<input type="text"  value="'+userAcc+'">');
                                    localStorage.setItem("updateCourseId",data.data.courseId);
                                    localStorage.setItem("url",data.data.url);
                                    localStorage.setItem("courseUrl",data.data.courseUrl);
                                    localStorage.setItem("userName",userName);
                                    localStorage.setItem("userAcc",userAcc);
                                },
                            })
                        }
                    });
                }
            });
        }
        /*表格修改结束*/
    }
);

//刷新
function Refresh() {
    location.reload();
}

//跳转
function index() {
    window.location.href = '/course/course-list.html';
}